
// 基础体温
var BBT_SHOW_NUM = 30;
var bbtDateArray = new Array();
var bbtTemperatureArray = new Array();

// 月经
var mensesYAxisArray = new Array();
var customerDataYjqStartDateArray = new Array();
var customerDataYjqEndDateArray = new Array();

var mensesStartDateArray = new Array();
var mensesEndDateArray = new Array();

var mensesDurationArray = new Array();
var mensesPeriodArray = new Array();
var mensesChartLabelArray = new Array();

var INTENT_SHOW_PERIOD_NUM = 6;

// 宫颈黏液
var MUCUS_SHOW_NUM = 16;
var mucusDateArray = new Array();
var mucusDataArray = new Array();

function initOverviewChartData() {
	initYjqStartDateArray();
    initYjqEndDateArray();
    saveCustomerDataYjqStartDateArrayStr2Local();
    saveCustomerDataYjqEndDateArrayStr2Local();
    
    initBBTLineChartData();
    initMensesBarChartData();
    initMucusBarChartData();
}


function initYjqStartDateArray() {
    for (var i = 0; i < customerDataJSONArrayLen; i++) {
        if (isPeriodStartDate(i)) {
            customerDate = customerDataJSONArray[i].date;

            if (isYjq(customerDate)){
            	addIntoYjqStartDateArray(customerDate);
            	
            	if(compareDateStr(customerDate, getCurrentDateStr()))
            		addIntoMensesStartDateArray(customerDate);
            }
        }
    }

}

function initYjqEndDateArray() {
    for (var i = 0; i < customerDataJSONArrayLen; i++) {
        if (isPeriodEndDate(i)) {
            customerDate = customerDataJSONArray[i].date;

            if (isYjq(customerDate)){
            	addIntoYjqEndDateArray(customerDate);
            	
            	if(compareDateStr(customerDate, getCurrentDateStr()))
            		addIntoMensesEndDateArray(customerDate);
            }
        }
    }

}

function isPeriodStartDate(i) {
    if (i == 0) return true;
    else {
        var previousCustomerPeriod = customerDataJSONArray[i - 1].period;
        var customerPeriod = customerDataJSONArray[i].period;

        if (previousCustomerPeriod != customerPeriod) return true;
        else return false;
    }
}

function isPeriodEndDate(i) {
    if (i == customerDataJSONArrayLen - 1) return true;
    else {
        var previousCustomerPeriod = customerDataJSONArray[i + 1].period;
        var customerPeriod = customerDataJSONArray[i].period;

        if (previousCustomerPeriod != customerPeriod) return true;
        else return false;
    }
}
function addIntoYjqStartDateArray(customerDate) {
    var yjqStartDateArrayLen = customerDataYjqStartDateArray.length;
    customerDataYjqStartDateArray[yjqStartDateArrayLen] = customerDate;
}

function addIntoMensesStartDateArray(customerDate){
	var mensesStartDateArrayLen = mensesStartDateArray.length;
    mensesStartDateArray[mensesStartDateArrayLen] = customerDate;
}

function addIntoYjqEndDateArray(customerDate) {
    var yjqEndDateArrayLen = customerDataYjqEndDateArray.length;
    customerDataYjqEndDateArray[yjqEndDateArrayLen] = customerDate;
}

function addIntoMensesEndDateArray(customerDate){
	var mensesEndDateArrayLen = mensesEndDateArray.length;
    mensesEndDateArray[mensesEndDateArrayLen] = customerDate;
}

function saveCustomerDataYjqStartDateArrayStr2Local(){
	var yjqStr = customerDataYjqStartDateArray.toString();
	var mensesStr = mensesStartDateArray.toString();
	localStorage.setItem("customerDataYjqStartDateArrayStrKey", yjqStr); 
	localStorage.setItem("customerDataMensesStartDateArrayStrKey", mensesStr); 
}

function saveCustomerDataYjqEndDateArrayStr2Local(){
	var yjqStr = customerDataYjqEndDateArray.toString();
	var mensesStr = mensesEndDateArray.toString();
	localStorage.setItem("customerDataYjqEndDateArrayStrKey", yjqStr); 
	localStorage.setItem("customerDataMensesEndDateArrayStrKey", mensesStr); 
}


/****************************************** 
  	 基础体温初始化
   *******************************************/

function initBBTLineChartData() {    
    bbtDateArray = getRoundDate(getCurrentDateStr(), BBT_SHOW_NUM, 0);

	for(var i = 0; i< BBT_SHOW_NUM; i++)
		bbtTemperatureArray[i] = customerDatePolishedTemperatureMap.get(bbtDateArray[i]);

    bbtTitleText = bbtTemperatureArray[--i] + '℃';       	
}

/****************************************** 
  	 月经初始化
   *******************************************/


function initMensesBarChartData() {
    mensesDurationArray = getMensesDurationArray();
    mensesPeriodArray = getMensesPeriodArray();
    initMensesYAxisArray();
    mensesChartLabelArray = getMensesChartLabelArray();
}

function getMensesChartLabelArray() {
    for (var i = 0; i < mensesDurationArray.length; i++) {
        mensesChartLabelArray[i] = mensesDurationArray[i] + "/" + mensesPeriodArray[i];
    }
    return mensesChartLabelArray;
}

//经期
function getMensesDurationArray() {
    var atualShowPeriodNum = getActualShowPeriod();
    //只需获取 Len - 1 个经期即可，因为周期个数为 Len - 1 个。
    for (var i = 0; i < atualShowPeriodNum; i++) {
        var tempStartDate = mensesStartDateArray[i];
        var tempEndDate = mensesEndDateArray[i];
        mensesDurationArray[i] = getDaysDuration(tempStartDate, tempEndDate);        
    }

    return mensesDurationArray;
}

//周期
function getMensesPeriodArray() {
    var atualShowPeriodNum = getActualShowPeriod();
    //只需获取 Len - 1 个经期即可，因为周期个数为 Len - 1 个。
    for (var i = 0; i < atualShowPeriodNum; i++) {
        var oldStartDate = mensesStartDateArray[i];
        var newStartDate = mensesStartDateArray[i + 1];
        mensesPeriodArray[i] = getDaysDuration(oldStartDate, newStartDate) - 1;
    }

    return mensesPeriodArray;
}

function initMensesYAxisArray(){
	for(var i = 0; i < INTENT_SHOW_PERIOD_NUM; i++)
		mensesYAxisArray[i] = "" + i;
}

function getActualShowPeriod() {
    var mensesStartDateArrayLength = mensesStartDateArray.length;
    var atualShowPeriodNum = getMin(INTENT_SHOW_PERIOD_NUM, mensesStartDateArrayLength - 1);
    
    return atualShowPeriodNum;
}

function getMin(m, n) {
    if (m <= n) return m;
    else return n;
}

/****************************************** 
  	 宫颈粘液初始化
   *******************************************/

function initMucusBarChartData() {
	mucusDateArray = getRoundDate(getCurrentDateStr(), MUCUS_SHOW_NUM, 0);
	
	for(var i = 0; i< MUCUS_SHOW_NUM; i++)
		mucusDataArray[i] = customerDateMucusMap.get(mucusDateArray[i]);     	
}


/****************************************** 
  	 画图区域初始化
   *******************************************/
function initOverviewChartZone(){	
	var screenHeight = window.innerHeight;
	var chartZonePerc = 0.4;
	
	document.getElementById('bbtLineChart').style.height = screenHeight * chartZonePerc + "px";
	document.getElementById('mensesBarChart').style.height = screenHeight * chartZonePerc + "px";
	document.getElementById('mucusBarChart').style.height = screenHeight * chartZonePerc + "px";
}